package com.avic.rong.utils;

import android.content.Context;
import android.os.Environment;

import com.avic.rong.wedgit.DeviceInfoUtil;

import java.io.File;

public class LocalStorageUtil {

    public static LocalStorageUtil getInstance(Context context) {
        if (instance == null) {
            instance = new LocalStorageUtil();
            instance.initLocalDir(context);
        }
        return instance;
    }

    private static LocalStorageUtil instance;

    // SD卡文件根目录
    private static String BASE_DIR = "data/tj/tcm";
    // 缓存目录
    private static final String FILE_CACHE_DIR = "caches";
    private static final String FILE_UPLOAD_DIR = "upload";


    // sd卡根目录
    private String mSdcardAbsolutePath;
    private String mImageCacheAbsoluteDir;// 图片缓存目录


    // 照片上传临时路径
    private String mUploadUserPhotoTempFilePath;// 未处理的jpg



    private void initLocalDir(Context context) {

        long availableSDCardSpace = DeviceInfoUtil.getExternalStorageSpace();// 获取SD卡可用空间

        String sdcardBasePath;

        if (availableSDCardSpace != -1L) {// 如果存在SD卡
            sdcardBasePath = Environment.getExternalStorageDirectory() + File.separator + BASE_DIR;
        } else if (DeviceInfoUtil.getInternalStorageSpace() != -1L) {
            sdcardBasePath = context.getFilesDir().getPath() + File.separator + BASE_DIR;
        } else {// sd卡不存在
            // 没有可写入位置
            return;
        }
        setSdcardAbsolutePath(sdcardBasePath);
        setImageCacheAbsoluteDir(sdcardBasePath + File.separator + FILE_CACHE_DIR);


        // 图片缓存目录

        // 初始化根目录
        File basePath = new File(getSdcardAbsolutePath());
        if (!basePath.exists()) {
            basePath.mkdirs();
        }

        // 初始化网络图片缓存目录
        File cachePath = new File(getImageCacheAbsoluteDir());
        if (!cachePath.exists()) {
            cachePath.mkdirs();
        }


        setUploadUserPhotoTempFilePath(sdcardBasePath + File.separator + "unhandled.jpg");

    }

    public String getSdcardAbsolutePath() {
        return mSdcardAbsolutePath;
    }

    /**
     * @param sdcardAbsolutePath
     */
    public void setSdcardAbsolutePath(String sdcardAbsolutePath) {
        this.mSdcardAbsolutePath = sdcardAbsolutePath;
    }

    public String getImageCacheAbsoluteDir() {
        return mImageCacheAbsoluteDir;
    }

    public void setImageCacheAbsoluteDir(String imageCacheAbsoluteDir) {
        this.mImageCacheAbsoluteDir = imageCacheAbsoluteDir;
    }

    public String getUploadUserPhotoTempFilePath() {
        return mUploadUserPhotoTempFilePath;
    }

    public void setUploadUserPhotoTempFilePath(String uploadUserPhotoTempFilePath) {
        this.mUploadUserPhotoTempFilePath = uploadUserPhotoTempFilePath;
    }



}
